/**
 * 功能描述
 */

define(function (require, exports, module) {
	var Grid = require("grid");
	
    var instance = {
        initComponent: function (params, widget) {
        	var grid = new Grid({
        		 parentNode: ".J-grid",
         	     autoRender: false,
         	    model: {
        	        url: "api/roomstatistics/querybytype",
        	        params: function() {
        	        	var subnav = widget.get("subnav");
        	        	var rangeDate = subnav.getValue("visitTime");
        	        	
        	            return {
        	            	"startDate":rangeDate.start,
        	            	"endDate":rangeDate.end,
        	            	fetchProperties:"roomType,count,incoming,pkRoomType"
        	            };
        	        },
        	        columns:[{
        	        	 name: "roomType",
        	             label: "房型"
        	        },{
        	        	 name: "count",
        	             label: "入住次数",
        	             format: "detail",
        	             formatparams: {
        	            	 key: "detail",
                     		 handler: function(index,data,rowEle){
                     			 console.log(data);
                     			var subnav = widget.get("subnav");
                	        	var rangeDate = subnav.getValue("visitTime");
                     			 widget.hide([".J-grid"]).show([".J-detailgrid"]);
                    			 widget.get("subnav").show(["cancel","visitTime"]);
                     			 widget.get("detailgrid").refresh({
                     				pkRoomType:data.pkRoomType,
                        	        "startDate":rangeDate.start,
                        	        "endDate":rangeDate.end,
                        	         fetchProperties:"roomCode,checkinDate,checkoutDate,incoming"
                     			 },function(data2){
                     				var j = 0;
                     				for(var i in data2){
                     					j+=(data2[i].incoming == null ? 0 : data2[i].incoming);
                     				}
                     				var k = parseFloat(j).toFixed(2);
                     				widget.get("detailgrid").setTitle("收入总金额:" + k + "元");
                     			 });
                     		}
        	             }
        	        },{
        	        	name: "incoming",
       	                label: "收入总金额(元)",
       	                format:function(val,row){
							return Number(val).toFixed(2);
						}
        	        }]
         	    }
        	});
        widget.set("grid",grid);
        }
    };
    module.exports = instance;
});
